package com.yunhai.api.controller;

import com.baomidou.mybatisplus.extension.api.ApiController;
import com.yunhai.common.core.controller.BaseController;
import com.yunhai.common.core.domain.AjaxResult;
import com.yunhai.common.core.page.TableDataInfo;
import com.yunhai.common.core.redis.RedisCache;
import com.yunhai.common.utils.StringUtils;
import com.yunhai.system.domain.Product;
import com.yunhai.system.service.BannerService;
import com.yunhai.system.service.NewsService;
import com.yunhai.system.service.ProductService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.Action;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/home")
public class HomeController extends BaseController {

    @Autowired
    private NewsService newsService;

    @Autowired
    private ProductService productService;

    @Autowired
    private RedisCache redisCache;


    /**
     * 搜索
     * @param param
     * @return
     */
    @PostMapping("/search")
    public AjaxResult search(@RequestBody Map<String, Object> param){
        String keyword = param.get("keyword") != null ? param.get("keyword").toString() : null;
        if(StringUtils.isBlank(keyword)){
            return AjaxResult.error("请输入搜索关键字");
        }
        Long page = MapUtils.getLong(param, "page", 1L);
        Long limit = MapUtils.getLong(param, "limit", 10L);
        Map<String, Object> result = new HashMap<>();
        TableDataInfo newsList = newsService.getNewsByKeyWord(keyword, page, limit);
        result.put("newsList", newsList);
        List<Product> productList = productService.productList(param);
        result.put("productList", productList);
        return AjaxResult.success(result);
    }


}
